/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.anote.ui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Logger;
import org.anote.Main;

/**
 *
 * @author Trilarion 2012
 */
public class TrayMenu {

    private static final Logger LOG = Logger.getLogger(TrayMenu.class.getName());

    private TrayMenu() {
    }

    public static void start() {
        final TrayIcon trayIcon;

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            Image image = Toolkit.getDefaultToolkit().getImage(Main.class.getClass().getResource("/icons/tray.png"));

            // TODO instead of PopupMenu create a custom JPopuMenu with icons and such
            PopupMenu menu = TrayMenu.createMenu();

            trayIcon = new TrayIcon(image, "A Note", menu);
            trayIcon.setImageAutoSize(true);

            trayIcon.displayMessage("Event", "A Note started", TrayIcon.MessageType.INFO);

            // left click on tray icon, open note browser
            MouseListener mouseListener = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent evt) {
                }
            };
            trayIcon.addMouseListener(mouseListener);

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                // TODO better error handling
                LOG.info("TrayIcon could not be added.");
            }

        } else {
            //  TODO error handling: System Tray is not supported
        }
    }

    /**
     * The popup menu of the tray.
     *
     * @return The popup menu.
     */
    private static PopupMenu createMenu() {

        PopupMenu menu = new PopupMenu();

        // new note menu item
        MenuItem newNoteItem = new MenuItem("New Note");
        newNoteItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        menu.add(newNoteItem);

        // note browser menu item
        MenuItem noteBrowserItem = new MenuItem("Note Browser");
        noteBrowserItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        menu.add(noteBrowserItem);

        menu.addSeparator();

        // synchronization menu item
        MenuItem synchroItem = new MenuItem("Synchronization");
        synchroItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        menu.add(synchroItem);

        menu.addSeparator();

        // options menu item
        MenuItem optionsItem = new MenuItem("Options");
        optionsItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        menu.add(optionsItem);

        // help menu item
        MenuItem helpItem = new MenuItem("Help");
        helpItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        menu.add(helpItem);

        menu.addSeparator();

        // exit item
        MenuItem exitItem = new MenuItem("Exit");
        exitItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Main.ShutDown();
            }
        });
        menu.add(exitItem);

        return menu;
    }
}
